<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container justify-between">
      <div>
        <el-switch v-model="multi" inactive-text="批量操作"></el-switch>
        <template v-if="multi">
          <el-button size="mini" type="success" @click="doOperate('','online','确定要上线吗？')">上线</el-button>
          <el-button size="mini" type="danger" @click="doOperate('','offline','确定要下线吗？')">下线</el-button>
          <el-button size="mini" type="danger" @click="doRemove('','确定要删除吗？')">删除</el-button>
        </template>
      </div>
      <div>
        <el-select @change='doSearch' style="width: 140px" class="filter-item" v-model="listQuery.nav_id" placeholder="全部分类">
          <el-option v-for="(item,index) in itemNavList" :key="item.id" :label="item.name"  :value="item.id"></el-option>
        </el-select>
        <el-input @keyup.enter.native="doSearch" style="width: 200px;" class="filter-item" placeholder="输入名称搜索"
                  v-model="listQuery.keyword">
        </el-input>
        <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="doSearch">搜索
        </el-button>
        <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus" @click="openEditor()">
          新增项目
        </el-button>
        <el-button class="filter-item" style="margin-left: 10px;" @click="" type="primary"  @click="toitemNav">
          分类管理
        </el-button>
      </div>
    </div>


    <el-table  ref="multipleTable" :data="list" v-loading="listLoading" element-loading-text="给我一点时间"
               border fit highlight-current-row style="width: 100%"  @selection-change="onSelectionChange" height="500">
      <el-table-column  v-if='multi' type="selection"  width="55"> </el-table-column>
      <el-table-column type="index"  width="50"> </el-table-column>
      <el-table-column align="left" width="80"  label="图片" >
        <template slot-scope="scope">
          <img :src="scope.row.img" alt="" style="width:60px;height:40px;vertical-align: middle;" />
        </template>
      </el-table-column>
      <el-table-column align="left"  label="项目名称" >
        <template slot-scope="scope">
          <span>{{scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left"  label="所属分类" >
        <template slot-scope="scope">
          <span>{{scope.row.item_nav_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" width="80"  label="原价" >
        <template slot-scope="scope">
          <span>{{scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" width="80"  label="现价" >
        <template slot-scope="scope">
          <span>{{scope.row.cur_price }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" width="80"  label="会员价" >
        <template slot-scope="scope">
          <span>{{scope.row.member_price }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="状态" >
        <template slot-scope="scope">
          <span v-if="scope.row.status==1" class="color-disable ">未上线</span>
          <span v-if="scope.row.status==2" class="color-success ">已上线</span>
        </template>
      </el-table-column>
      <el-table-column align="left"   label="上线时间" >
        <template slot-scope="scope">
          <span>{{scope.row.online_time|beautiful_time }}</span>
        </template>
      </el-table-column>
      <el-table-column  align="left" label="操作"   class-name="small-padding fixed-width" fixed="right" width="240">
        <template slot-scope="scope">
          <el-button round size="mini" type="primary"  @click="openEditor(scope.row.id)">编辑</el-button>
          <el-button round v-if="scope.row.status=='2'" size="mini" type="warning"  @click="doOperate(scope.row.id,'offline','确定要下线'+scope.row.name+'吗？')">下线</el-button>
          <el-button round v-if="scope.row.status=='1'" size="mini" type="success"  @click="doOperate(scope.row.id,'online','确定要上线'+scope.row.name+'吗？')">上线</el-button>
          <el-button round size="mini" type="danger" @click="doRemove(scope.row.id,'确定要删除【'+scope.row.name+'】吗？')">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="onSizeChange" @current-change="onCurrentChange"
                     :current-page="listQuery.page" :page-sizes="[12,50,100,10000]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>


    <el-dialog title="编辑项目" :visible.sync="dialogFormVisible">
      <el-form :rules="formRules" ref="dataForm" :model="bean" label-position="left" label-width="80px" style='width: 400px; margin-left:50px;'>
        <template>
          <el-form-item  label="项目名称" prop="name">
            <el-input v-model="bean.name" placeholder="输入项目名称" ></el-input>
          </el-form-item>
          <el-form-item  label="所属分类" prop="nav_id">
            <el-select v-model="bean.nav_id" placeholder="请选择分类">
              <el-option 	v-for="item in itemNavList" :key="item.id" 	:label="item.name" :value="item.id"> 	</el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input v-model="bean.sort"  placeholder="显示顺序，默认为 0" ></el-input>
          </el-form-item>
          <el-form-item  label="图标" prop="img">
            <el-upload class="avatar-uploader" action="" :http-request="onUpload" :show-file-list="false" >
              <img v-if="bean.img"  :src="bean.img" class="avatar" >
              <i  class="el-icon-plus avatar-uploader-icon" v-else></i>
            </el-upload>
          </el-form-item>
        </template>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="saveBean" :loading="dealing">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
  import Vue from 'vue'
  import waves from '@/directive/waves' // 水波纹指令
  import API from '@/api'
  import Http from '@/http'
  import Store from '@/store'
  import Msg from '@/utils/msg'
  import Utils from '@/utils/common'
  import _ from 'underscore'
  import Qiniu from '@/utils/qiniu'

  export default {
    directives: {waves},
    data() {
      return {

        bean:{},
        dealing: false,
        dialogFormVisible: false,
        multi: false,
        list: [],
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 12,
          keyword: '',
          status: ''
        },
        multipleSelection: [],

        onlineStatus: [{data_value: 'all', data_text: '全部状态'}].concat(Store.getters.basedata.online_status),

        itemNavList:[],

        formRules: {
          name: [{ required: true, message: '请填写项目名称', trigger: 'blur' }],
        }

      }
    },
    filters: {},
    components: {},
    created() {
      this.loadList()
      this.loaditemNavList();

    },
    computed: {
    },
    methods: {

      saveBean() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dealing = true
            Http.getModel(API.save_item, this.bean, false).then((json) => {
              this.dealing = false
              if (json.code == 0) {
                this.dialogFormVisible = false
                Msg.success(json.msg, () => {
                  this.loadList()
                })
              } else {
                Msg.error(json.msg)
              }

            })
          }
        })
      },

      openEditor( id) {
        this.dialogFormVisible = true
        if (id) {
          this.loadBean(id)
        }else{
          this.bean = {
            sort: 0,
            img:""
          }
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      toitemNav() {
        this.$router.push({path: 'item_nav'})
      },

      doRemove(ids, tips) {
        if (!ids) {
          if (this.multipleSelection.length == 0) {
            Msg.error("请先选择");
            return false;
          } else {
            ids = _.pluck(this.multipleSelection, "id").join(",");
          }
        }
        Msg.confirm(tips, () => {
          Http.getModel(API.remove_item, {ids: ids}, false).then((json) => {
            if (json.code == 0) {
              Msg.success(json.msg, () => {
                this.loadList()
              })
            } else {
              Msg.error(json.msg)
            }
          })
        })
      },
      doOperate(ids, cmd, tips) {
        var status = 0;
        var url = "";
        switch (cmd) {
          case 'offline':
            status = 1;
            url = API.offline_item;
            break;
          case 'online':
            status = 2;
            url = API.online_item;
            break;
          default:
            break;
        }
        if (status == 1 || status == 2) {
          if (!ids) {
            if (this.multipleSelection.length == 0) {
              Msg.error("请先选择");
              return false;
            } else {
              ids = _.pluck(this.multipleSelection, "id").join(",");
            }
          }
          Msg.confirm(tips, () => {
            Http.getModel(url, {ids: ids, status: status}, false).then((json) => {
              if (json.code == 0) {
                Msg.success(json.msg, () => {
                  this.loadList()
                })
              } else {
                Msg.error(json.msg)
              }
            })
          })
        }
      },
      loadBean(id) {
        Http.getModel(API.get_item, {id: id}).then((json) => {
          if (json.code == 0) {
            let bean = json.data

            this.bean = bean
          }
        })
      },
      loadList() {
        this.listLoading = true
        Http.getModel(API.get_item_list, this.listQuery, false).then((json) => {
          this.loading = false
          if (json.code == 0) {
            this.list = json.data.list
            this.total = parseInt(json.data.total)
            this.listLoading = false
          }
        })
      },
      loaditemNavList() {
        this.listLoading = true
        Http.getModel(API.get_item_nav_list, {page: 1,limit: 1000,}, false).then((json) => {
          if (json.code == 0) {
            this.itemNavList = [{id:'all',name:'全部分类'}].concat(json.data.list)
          }
        })
      },

      onUpload(obj){
        var that = this;
        new Qiniu().upload(obj.file,(url)=>{
          that.bean.img= url
        })

      },

      /***   公用部分 start ***/
      onSelectionChange(val) {
        this.multipleSelection = val;
      },
      doSearch() {
        this.listQuery.page = 1
        this.loadList()
      },
      onSizeChange(val) {
        this.listQuery.limit = val
        this.loadList()
      },
      onCurrentChange(val) {
        this.listQuery.page = val
        this.loadList()
      },
      /***   公用部分 end ***/


    }
  }
</script>
